package com.example.seakdreamandroid;

import static com.example.seakdreamandroid.Constant.BASE_URL;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.seakdreamandroid.entity.User;
import com.example.seakdreamandroid.myInfo.MyInfo;
import com.github.appintro.AppIntro;

import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


//张子辉
public class AppIntroActivity extends AppIntro {
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    User user;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_layout);
        user = (User) getApplication();
        sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        //addSlide(new MyFragment());
        //自动登录
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .build();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL + "UserInfo/test")
                .post(requestBody)
                .addHeader("token", sharedPreferences.getString("token", ""))
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("okhttp", "请求失败");
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                try {
                    JSONObject result = new JSONObject(str);
                    if (result.getString("code").equals("404")){
                        Intent intent = new Intent();
                        intent.setClass(AppIntroActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }else if (result.getString("code").equals("200")){
                        String userId = result.getString("data");
                        editor.putString("token", result.getString("token"));
                        editor.commit();
                        Intent intent = new Intent();
                        intent.setClass(AppIntroActivity.this, MainActivity.class);
                        user.setUserId(userId);
                        setVip();
                        startActivity(intent);
//                            if (password.equals(userInfo.getString("password"))){
//                                Intent intent = new Intent();
//                                intent.setClass(LoginActivity.this, MainActivity.class);
////                                intent.putExtra("userId", userInfo.getString("userId"));
//                                user.setUserId(userInfo.getString("userId"));
//                                startActivity(intent);
//                            }else{
//                                Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
//                            }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });
        setImmersiveMode();
        setButtonsEnabled(false);
    }


    private void setVip(){
        //2.创建请求对象（Request）,不需要设置请求体
        Request request = new Request.Builder()
                //设置请求的地址，默认的请求方式是Get请求
                .url(BASE_URL+"UserInfo/getUserInfo"+"?id="+user.getUserId())
                //创建Request对象
                .build();
        //3.发送请求，获取响应
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        //发起异步的请求，不需要手动创建子线程
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败时回调
                Log.e("OkHttp","onFailure");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功时回调
                String reslut = response.body().string();
                runOnUiThread(new Runnable() {
                    public void run() {
                        try {
                            JSONObject object = new JSONObject(reslut);
                            JSONObject data = object.getJSONObject("data");
                            user.setIf_vip(data.getString("ifVip"));
                            Log.i("aaa", "adasdasd");
                            user.setPoints(data.getInt("points"));
                        }catch (Exception e){
                            e.printStackTrace();
                        }//setview
                    }
                });
            }
        });
    }
}
